/*
 * Filename: SchedulerSystem.java
 * Author: Michael Palmer
 * TCSS 342 Spring
 * May 29, 2011
 * Testing
 * 
 * Change Log:
 * Version May 29, 2011:
 * Made Constructor
 * 
 * Version May 30, 2011:
 * Random tests and edits 
 * 
 * Version May 31, 2011:
 * Wrote writeToFile method
 * 
 */
package main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Controls the Scheduler program.
 * 
 * @author Michael Palmer
 * @version May 29, 2011
 */
public class SchedulerSystem {

  //Constants
  public static final int MAX_TEACHER_CREDIT_HOURS = 15;
    
  //Fields

  private UserList my_users;
  private List<Course> my_courses;
  private File my_schedule_file;
  private File my_output_file;
  /**
   * A Scheduler object used for checking constraints
   */
  private Scheduler my_scheduler;
  /**
   * A Feedback object used for getting output
   */
  private Feedback my_feedback;
  
  //Constructors
  /**
   * Constructs an instance of the Scheduler System
   * pre: The specified files exist
   * post: The SchedulerSystem object will be constructed properly
   * 
   * @param the_sched_filename Filename for the schedule
   * @param the_prefs_filename Filename for the preferences
   * @throws NullPointerException when given a null file
   */
  public SchedulerSystem(final String the_sched_filename, 
      final String the_prefs_filename, final String the_output_filename) 
  throws NullPointerException{
    my_schedule_file = new File(the_sched_filename);
    my_output_file = new File(the_output_filename);
    getFileLines(my_schedule_file);
    my_courses = createCourseList(getFileLines(my_schedule_file));

    my_users = null;
    try {
      my_users = new UserList(new File(the_prefs_filename));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }        
    my_scheduler = new Scheduler(my_courses, my_users);    
  }
  
  /**
   * Creates a list of courses form fileLines
   * @param fileLines lines parsed from a schedule file
   * @return courselist A list of courses
   */
  public List<Course> createCourseList(
      List<String> fileLines) {
    List<Course> courselist = new ArrayList<Course>();
    for(String s : fileLines){
      Scanner scan = new Scanner(s).useDelimiter(",");
      String id = scan.next().trim();
      String section = scan.next().trim();
      String title = scan.next().trim();
      String prof = scan.next();
      String days = scan.next().trim();
      String start = scan.next().trim();
      String end = scan.next().trim();
      String cred_string = scan.next().trim();
      int credits = 0;
      if (!cred_string.equals("TBA")){
        credits = Integer.parseInt(cred_string);
      }            
      courselist.add(new Course(id, section, title,
          prof, days, start, end, credits));
    }    
    return courselist;
  }

  //Methods
  /**
   * Parses lines from a specified file into a list of strings.
   * @param the_file The file to parse
   */
  public List<String> getFileLines(final File the_file){
    List<String> new_list = new ArrayList<String>();
    Scanner new_scan;
    
    try{
    new_scan = new Scanner(the_file);
    }
    catch (FileNotFoundException e){
      System.err.println("Filename: " + the_file.getAbsolutePath());
      return new_list;
    }
    while(new_scan.hasNext()){
      String line = new String(new_scan.nextLine());
      if(!line.contains("%") && !line.contains("COURSE")){
          new_list.add(line);
      }        
    }    
    return new_list;
  }
  
  /**
   * Returns my courses
   * @return my_courses the course list
   */
  public List<Course> getCourseList()
  {
    return my_courses;
  }  
  
  /**
   * Returns a list of users
   * @return my_users a UserList object
   */
  public UserList getUsers(){
      return my_users;
  }
  
  /**
   * Returns the scheduler object.
   * @return my_scheduler a Scheduler object
   */
  public Scheduler getScheduler(){
    return my_scheduler;
  }
  
  /**
   * Writes violated preferences to the output file.
   * Also outputs to the console.
   */
  public void writeToFile() {
     
    if(my_feedback == null) {
      getFeedback();
    }
    
    PrintStream output = null;
    try {
      output = new PrintStream(my_output_file);
    } catch (FileNotFoundException e) {
      System.out.println("Unable to create output file!!!");
      return;
    }   
    System.out.println("OUTPUT\n" + my_feedback.toString());
    output.print(my_feedback.toString());
    
  }
  
  public Feedback getFeedback() {
    my_feedback = my_scheduler.checkConstraints();
    
    return my_feedback;
  }
  
  /**
   * The main method.  Runs the program.
   * Files should be entered in this order:
   * 1. Schedule
   * 2. Preferences
   * 3. Output
   * 
   * @param args commandline arguments
   */
  public static void main(String[] args) {    
  
    String outputfile_name = new String(args[2]);
    
    SchedulerSystem s = 
      new SchedulerSystem(args[0], args[1],
          outputfile_name);
    
    s.getFeedback();

    s.writeToFile();
       
  }
}